<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <el-col>操作</el-col>
            </el-row>
          </el-col>
        </el-row>
      </el-card>
      <el-tree
        :data="list"
        :props="defaultProps"
        :default-expand-all="true"
      >
        <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
        <!--
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (table 单元格数据渲染)
    -->
        <template #default="{ data }">
          <el-row
            type="flex"
            justify="space-between"
            align="middle"
            style="height: 40px; width: 100%;"
          >
            <el-col :span="20">
              <span>{{ data.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>{{ data.manager }}</el-col>
                <el-col>
                  <!-- 下拉菜单 element -->
                  <el-dropdown>
                    <span> 操作<i class="el-icon-arrow-down" /> </span>
                    <!-- 下拉菜单 -->
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="addPart(data)">添加子部门</el-dropdown-item>
                      <el-dropdown-item @click.native="editDepart(data)">编辑部门</el-dropdown-item>
                      <el-dropdown-item @click.native="delbtn(data.id)">删除部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <AddDept
      ref="addDep"
      :show-dialog="showDialog"
      :cur-node="curNode"
      :all-list="list"
      @close-dialog="handleClose"
      @update-depart="hGetDepartments"
    />
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/tranListDepartments'
import AddDept from './components/add-dept'
export default {
  components: {
    AddDept
  },
  data() {
    return {
      list: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      showDialog: false,
      curNode: {}
    }
  },
  mounted() {
    // 获取数据
    this.hGetDepartments()
  },
  methods: {
    async hGetDepartments() {
      const res = await getDepartments()
      //   console.log(res)
      this.list = tranListToTreeData(res.data.depts)
    },
    delbtn(id) {
      this.$confirm('你确认要进行删除么?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 删除操作成功
        await delDepartments(id)
        // 添加提示
        this.$message.success('删除操作成功')
        // 更新数据
        this.hGetDepartments()
      }).catch(error => {
        console.log(error)
      })
    },
    handleClose() {
      this.showDialog = false
    },
    addPart(data) {
      console.log(data)
      this.showDialog = true
      this.curNode = data
    },
    // 编辑
    editDepart(data) {
      this.showDialog = true
      this.curNode = data
      console.log(this.curNode)

      this.$refs.addDep.hGetDepartments(data.id)
    }
  }
}
</script>

<style>

</style>
